package com.mybatis.jpa.statement;

import java.util.HashMap;
import java.util.Map;

import com.mybatis.jpa.statement.builder.*;
import com.mybatis.jpa.type.MethodConstants;

/**
 * statementBuider holder and adapter</br>
 * holder : ensure every {@link StatementBuildable} is singleton</br>
 * adapter: {@link #adapted(String)}
 *
 * @author svili
 * @data 2017年5月8日
 *
 */
public class StatementBuilderHolder {

	/** key is methodType @{link MethodConstants} */
	private static Map<String, StatementBuildable> builderMap;

	/** 私有化构造方法,不允许创建实例对象 */
	private StatementBuilderHolder() {
	}

	static {
		// ensure every {@link StatementBuildable} is singleton
		builderMap = new HashMap<String, StatementBuildable>();
		builderMap.put(MethodConstants.INSERT_SELECTIVE, new InsertSelectiveBuilder());
		builderMap.put(MethodConstants.INSERT, new InsertBuilder());
		builderMap.put(MethodConstants.BATCH_INSERT, new BatchInsertBuilder());
		builderMap.put(MethodConstants.DELETE, new DeleteBuilder());
		builderMap.put(MethodConstants.UPDATE_ADVANCE, new UpdateAdvaceBuilder());
		builderMap.put(MethodConstants.UPDATE_SELECTIVE, new UpdateSelectiveBuilder());
		builderMap.put(MethodConstants.UPDATE, new UpdateBuilder());
		builderMap.put(MethodConstants.BATCH_UPDATE_INCLUDE_NULL, new BatchUpdateIncludeNullBuilder());
		builderMap.put(MethodConstants.BATCH_UPDATE, new BatchUpdateBuilder());
		builderMap.put(MethodConstants.SELECT, new SelectBuilder());
		builderMap.put(MethodConstants.SELECT_PAGE, new SelectPageBuilder());
		builderMap.put(MethodConstants.SELECT_COUNT, new SelectCountBuilder());
		builderMap.put(MethodConstants.SELECT_BEAN, new SelectBeanBuilder());
		builderMap.put(MethodConstants.SELECT_ADVANCE, new SelectAdvanceBuilder());
		builderMap.put(MethodConstants.SELECT_COUNT_ADVANCE, new SelectCountAdvanceBuilder());
		builderMap.put(MethodConstants.DELETE_BEAN, new DeleteBeanBuilder());
		builderMap.put(MethodConstants.SELECT_NESTED, new SelectNestedBuilder());
		builderMap.put(MethodConstants.SELECT_NESTED_FOR_ORDER, new SelectNestedForOrderByBuilder());
		builderMap.put(MethodConstants.SELECT_PAGE_FOR_ORDER, new SelectPageForOrderByBuilder());
		builderMap.put(MethodConstants.SELECT_BY_IDS, new SelectByIdsBuilder());

	}

	/**
	 * 注册sql构造器
	 * @param type 类型
	 * @param buildable 构造器对象
	 */
	public static  void register(String type,StatementBuildable buildable)
	{
		builderMap.put(type,buildable);
	}

	/**
	 * adapted StatementBuilder whith methodType @{link MethodConstants}
	 */
	public static StatementBuildable adapted(String methodType) {

		if(builderMap.containsKey(methodType))
		{
			return builderMap.get(methodType);
		}
		return null;
	}
}
